//
//  ImageLoading.m
//  UILesson17_1图片异步加载
//
//  Created by lanou3g on 16/1/13.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ImageLoading.h"

static ImageLoading *imageLoading = nil;
@implementation ImageLoading
// 单例
+ (instancetype)shareImageLoading
{
    @synchronized(self) {
        if (imageLoading == nil) {
            imageLoading = [[ImageLoading alloc] init];
        }
        return imageLoading;
    }
}

// 实现图片加载和block回传的方法
- (void)imageLoadingWithString:(NSString *)urlString returnImageBlock:(imageLoadingWithBlock) returnImageBlock
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            returnImageBlock(data);
        });
    }];
    // 执行任务
    [dataTask resume];
}


@end
